#import "AdParams.h"
#import "BwtSplashAdManager.h"
#import "BwtInterstitialAdManager.h"
#import "BwtNativeExpressAdManager.h"
#import "BwtAdLogManager.h"
#import "BwtAdEventManager.h"
#import "BwtAdFlutterPlugin.h"
#import "BwtSplashAd.h"
#import "CXHAdSDK-Core/CXHAdSDK.h"
#import "BwtNativeExpressAdFactory.h"
#import "BwtBannerAdManager.h"
#import "BwtBannerAdFactory.h"
#import "BwtServiceAdManager.h"
#import "BwtServiceAdFactory.h"
#import "BwtNativeAdManager.h"
#import "BwtNativeAdFactory.h"
@implementation BwtAdFlutterPlugin

+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
    FlutterMethodChannel* channel = [FlutterMethodChannel
                                     methodChannelWithName:@"bwt_ad_flutter"
                                     binaryMessenger:[registrar messenger]];
    BwtAdFlutterPlugin* instance = [[BwtAdFlutterPlugin alloc] init];
    [registrar addMethodCallDelegate:instance channel:channel];
    
    /// 注册 BwtNativeExpressAdFactory
    [registrar registerViewFactory:[[BwtNativeExpressAdFactory alloc] initWithMessenger:registrar.messenger] withId:@"BwtNativeExpressAd"];
    ///注册banner广告Factory
    [registrar registerViewFactory:[[BwtBannerAdFactory alloc] initWithMessenger:registrar.messenger] withId:@"viewTypeBwtBannerAd"];
    ///注册非标广告Factory
    [registrar registerViewFactory:[[BwtServiceAdFactory alloc] initWithMessenger:registrar.messenger] withId:@"viewTypeBwtServiceAd"];
    ///注册信息流自渲染
    [registrar registerViewFactory:[[BwtNativeAdFactory alloc] initWithMessenger:registrar.messenger] withId:@"viewTypeBwtNativeAd"];
    
    /// 设置FlutterMethodChannel到BwtAdEventManager
    BwtAdEventManager *bwtAdEventManager = [BwtAdEventManager getBwtAdEventManager];
    [bwtAdEventManager setFlutterMethodChannel:channel];
    
    // 设置默认是否需要日志
    [BwtAdLogManager getBwtAdLogManager].isNeedLog = YES;
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
    NSDictionary *arguments = call.arguments;
    
    [[BwtAdLogManager getBwtAdLogManager] log:@"当前调用的原生方法为: %@, 请求参数为: %@", call.method, arguments];
    
    NSString *appId = arguments[@"appId"];
    NSString *logType = arguments[@"logType"];
    
    AdParams *adParams = [AdParams new];
    [adParams handleInit:arguments];
    
    if ([@"init" isEqualToString:call.method]) {
        NSNumber *isRequestIdfa = arguments[@"isRequestIdfa"];
        _isRequestIdfa = [@(1) isEqualToNumber:isRequestIdfa];
        
        /// 初始化广告SDK
        [self handleInit:appId logType:logType];
    } else if ([@"loadSplashAd" isEqualToString:call.method]) {
        /// 加载开屏广告
        BwtSplashAdManager *bwtSplashAdManager = [BwtSplashAdManager getBwtSplashAdManager];
        [bwtSplashAdManager loadSplashAd:adParams isRequestIdfa:_isRequestIdfa];
    } else if ([@"loadNativeExpressAd" isEqualToString:call.method]) {
        /// 加载信息流模板广告
        BwtNativeExpressAdManager *bwtNativeExpressAdManager = [BwtNativeExpressAdManager getBwtNativeExpressAdManager];
        [bwtNativeExpressAdManager loadNativeExpressAd:adParams isRequestIdfa:_isRequestIdfa];
    } else if ([@"loadInterstitialAd" isEqualToString:call.method]) {
        /// 加载插屏广告
        BwtInterstitialAdManager *bwtInterstitialAdManager = [BwtInterstitialAdManager getBwtInterstitialAdManager];
        [bwtInterstitialAdManager loadInterstitialAd:adParams loadThenShow:NO isRequestIdfa:_isRequestIdfa];
    } else if ([@"showInterstitialAd" isEqualToString:call.method]) {
        /// 展示插屏广告
        BwtInterstitialAdManager *bwtInterstitialAdManager = [BwtInterstitialAdManager getBwtInterstitialAdManager];
        [bwtInterstitialAdManager showInterstitialAd:adParams];
    } else if ([@"loadThenShowInterstitialAd" isEqualToString:call.method]) {
        /// 加载并展示插屏广告
        BwtInterstitialAdManager *bwtInterstitialAdManager = [BwtInterstitialAdManager getBwtInterstitialAdManager];
        [bwtInterstitialAdManager loadInterstitialAd:adParams loadThenShow:YES isRequestIdfa:_isRequestIdfa];
    } else if ([@"releaseAd" isEqualToString:call.method]) {
        /// 释放插屏广告
        BwtInterstitialAdManager *bwtInterstitialAdManager = [BwtInterstitialAdManager getBwtInterstitialAdManager];
        [bwtInterstitialAdManager releaseAd:adParams.adId];
        
        /// 释放信息流广告
        BwtNativeExpressAdManager *bwtNativeExpressAdManager = [BwtNativeExpressAdManager getBwtNativeExpressAdManager];
        [bwtNativeExpressAdManager releaseAd:adParams.adId];
        
        /// 释放开屏广告
        BwtSplashAdManager *bwtSplashAdManager = [BwtSplashAdManager getBwtSplashAdManager];
        [bwtSplashAdManager releaseAd:adParams.adId];
        
        /// 释放自渲染广告
        BwtNativeAdManager *bwtNativeAdManager = [BwtNativeAdManager getBwtNativeAdManager];
        [bwtNativeAdManager releaseAd:adParams.adId];
        
        /// 释放非标广告
        BwtServiceAdManager *bwtServiceAdManager = [BwtServiceAdManager getBwtBannerAdManager];
        [bwtServiceAdManager releaseAd:adParams.adId];
        
    }else if ([@"loadBannerAd" isEqualToString:call.method]) {
        BwtBannerAdManager *bannerManager = [BwtBannerAdManager getBwtBannerAdManager];
        [bannerManager loadBannerAd:adParams isRequestIdfa:YES];
    }else if ([@"loadServiceAd" isEqualToString:call.method]){
        BwtServiceAdManager *serviceManager = [BwtServiceAdManager getBwtBannerAdManager];
        [serviceManager loadServiceAd:adParams isRequestIdfa:YES];
    }else if ([@"loadNativeAd" isEqualToString:call.method]){
        BwtNativeAdManager *manager = [BwtNativeAdManager getBwtNativeAdManager];
        [manager loadNativeAd:adParams isRequestIdfa:YES];
    }else if ([@"nativeAdCloseAd" isEqualToString:call.method]){
        BwtNativeAdManager *manager = [BwtNativeAdManager getBwtNativeAdManager];
        [manager closeAd:adParams.adId];
    }
    result(@YES);
}

/// 初始化广告SDK
- (void)handleInit:(NSString*)appId logType:(NSString *)logType{
    /// 设置请求域名
//    [CXHAdSDK setRequestDomain:@"https://ad.bwton.com"];
    
    if ([@"INFO" isEqualToString:logType]) {
        [CXHAdSDK setLogLevel:CXHSDKLogLevelInfo];
    } else if ([@"DEBUG" isEqualToString:logType]) {
        [CXHAdSDK setLogLevel:CXHSDKLogLevelDebug];
    } else if ([@"WARN" isEqualToString:logType]) {
        [CXHAdSDK setLogLevel:CXHSDKLogLevelWarning];
    } else if ([@"ERROR" isEqualToString:logType]) {
        [CXHAdSDK setLogLevel:CXHSDKLogLevelError];
    } else {
        [BwtAdLogManager getBwtAdLogManager].isNeedLog = NO;
        [CXHAdSDK setLogLevel:CXHSDKLogLevelOff];
    }
    
    [CXHAdSDK setLogLevel:CXHSDKLogLevelError];
    /// 初始化广告SDK
    [CXHAdSDK initWithAppId:appId requestDomain:@"https://ad.bwton.com" completionBlock:^(NSError * _Nullable error) {
        if (error) {
            [[BwtAdLogManager getBwtAdLogManager] log:@"init error, error is: %@", error];
        } else {
            [[BwtAdLogManager getBwtAdLogManager] log:@"init success!"];
        }
    }];
}

@end
